<?php
/*
 * @Author: zhx zhx.0830@qq.com
 * @Date: 2023-08-28 09:27:15
 * @LastEditors: zhx zhx.0830@qq.com
 * @LastEditTime: 2023-09-06 17:29:13
 * @FilePath: \cn-saas-new\app\Http\Requests\AdminuserRequest.php
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

namespace App\Http\Requests;

use Illuminate\Contracts\Validation\Validator;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Support\Str;

/**
 * 应用权限
 *
 * @Desc:验证请求类
 *
 * @author: zhx
 *
 * @Time: 2023/8/10 10:37
 */
class ApplicationAuthRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * 获取应用于请求的验证规则。
     *
     * @return array<string, \Illuminate\Contracts\Validation\Rule|array|string>
     */
    public function rules(): array
    {
        $r = [
            'id' => '',
            'parent_id' => 'required',
            'name' => 'required',
            'type' => 'required',
            'http_path' => '',
            'icon' => '',
            'order' => 'required',
        ];

        return $r;
    }

    public function messages(): array
    {
        $m = [
            'logo.required' => '请上传LOGO',
            'name.required' => '权限名称名称不能为空',
            'parent_id.required' => '请选择父级',
            'type.required' => '类型不能为空',
            'http_path.required' => '链接地址不能为空',
            'order.required' => '排序不能为空',
        ];

        return $m;
    }

    public function all($keys = null)
    {
        $data = parent::all($keys);

        $data['slug'] = md5((string) Str::uuid());

        return $data;
    }

    protected function failedValidation(Validator $validator)
    {
        $error = $validator->errors()->all();
        throw new HttpResponseException(response()->json(['msg' => $error[0], 'code' => '500', 'data' => 'error'], 500));
    }
}
